package org.team4.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.team4.domain.Intercom;
import org.team4.exception.CRUDException;
import org.team4.util.LoginBean;

import org.team4.dao.GenericDAO;
import org.team4.dao.IntercomDAO;

public class IntercomDAO extends GenericDAO<Intercom> {

	private static final long serialVersionUID = -6016416637756110162L;
	private static IntercomDAO instance = null;
	
	public static IntercomDAO getInstance(SessionFactory sessionFactory) {
		if(instance == null) {
            instance =  new IntercomDAO(sessionFactory);
        }
        return instance;
	}
	
	private IntercomDAO(SessionFactory sessionFactory) {
		super(sessionFactory, Intercom.class);
	}
	
	/**
	 * 查找未读数量
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public Integer countNoRead() throws CRUDException {
		Query query = getSession().createQuery("select count(id) from Intercom where owner = ? and readFlag = 'F'");
		query.setParameter(0, LoginBean.getLoginBean().getUser());
		return ((Long)query.uniqueResult()).intValue();
	}

}
